import React, {Component} from 'react';
import {
    StyleSheet,
    ScrollView,
    Text,
    View,
    ImageBackground,
    FlatList,
    RefreshControl,
    TouchableOpacity,
    Alert,
    ActivityIndicator,
} from 'react-native';

import moment from 'moment';
import {Button, Flex, InputItem, Modal, Tabs, Toast, WhiteSpace, WingBlank} from '@ant-design/react-native';
import {activity} from '~/api/activity';
import {getProjectDataByCompany} from '~/api/project';
import {getParkingPlanList, setPlanAgentState} from '~/api/parking';
import HCard from '~/components/h-card';
import {getHttpUrl} from '~/api/basic';
import {setCompanyBuildVerify} from '~/api/company';
import {connect} from 'react-redux';
const mapGetter = (state) => {
    return {
        userinfo: state.userinfo,
        system: state.system,
    };
};
let pagecount = 0;
let index = 1;
let size = 10;
class Activity extends Component {
    static navigationOptions = {
        title: '营销活动',
    };
    constructor(props) {
        //调用基类的所有的初始化方法
        super(props);
        // 设置当前组件的属性
        this.state = {
            list: [],
            state: 1,
            page_index: 1, //页
            page_size: 10, //每页条数
            isLoading: false, //加载
            refreshing: false,
            footer: 0, //上拉底部显示
            baseurl: '',
            tabs: [
                {title: '全部活动', value: 0},
                {title: '待审核', value: 6},
                {title: '待进行', value: 12},
                {title: '进行中', value: 11},
                {title: '已完成', value: 13},
            ],
            tabkey: 'all',
            visible: false,
            textinfo: '',
            parkingplanid: '',
        };
        this.TabClick = this.TabClick.bind(this);
        getHttpUrl().then((r) => {
            if (r.code === 0) {
                this.setState({
                    baseurl: r.data,
                });
            }
        });
        this.onClose = () => {
            this.setState({
                visible: false,
            });
        };
    }
    TabClick(tab) {
        this.setState(
            {
                tabkey: tab.value === 0 ? 'all' : tab.value,
            },
            () => {
                this.getplanlist();
            },
        );
    }
    setTextinfo = (text) => {
        this.setState({
            textinfo: text,
        });
    };
    comfire(id) {
        this.setState({
            visible: true,
            parkingplanid: id,
        });
    }
    comfireactivity(state) {
        setPlanAgentState({
            id: this.state.parkingplanid, //营销方案id
            state: state,
            note: this.state.textinfo,
        }).then((r) => {
            this.onClose();
            if (r.code === 0) {
                this.getplanlist();
            } else {
                Toast.info(r.msg);
            }
        });
    }
    getplanlist() {
        getParkingPlanList({
            page_index: this.state.page_index,
            page_size: this.state.page_size,
            condidate: {
                tabkey: this.state.tabkey,
            },
        }).then((r) => {
            if (r.code === 0) {
                this.setState({
                    list: r.data.list,
                    footer: 0,
                });
                if (this.state.page_index === 1) {
                    pagecount = r.data.total_page_count;
                    this.setState({
                        footer: r.data.total_item_count < 10 ? 2 : 0,
                    });
                }
            }
        });
    }
    _onRefresh() {
        this.setState({footer: 0, page_index: index, page_size: size}, () => {
            this.getplanlist();
        });
    }

    //上拉加载更多
    loadData = () => {
        this.setState({isLoading: false});
        let dataArray = [];
        if (pagecount - 1 > 0) {
            pagecount = pagecount - 1;
            this.setState(
                {
                    page_index: this.state.page_index + 1,
                    footer: 1,
                },
                () => {
                    getParkingPlanList({
                        page_index: this.state.page_index,
                        page_size: this.state.page_size,
                        condidate: {
                            tabkey: this.state.tabkey,
                        },
                    }).then((r) => {
                        if (r.code === 0) {
                            dataArray = r.data.list;
                            dataArray = this.state.list.concat(dataArray);
                            this.setState({
                                list: dataArray,
                                isLoading: false,
                            });
                        } else {
                            console.log(r);
                        }
                    });
                },
            );
        } else {
            this.setState({
                footer: 2,
            });
        }
    };

    //上拉底部显示
    genIndicator() {
        if (this.state.footer === 1) {
            return (
                <View style={styles.indicatorContainer}>
                    <ActivityIndicator style={styles.indicator} size={'large'} animating={true} />
                    <Text style={{textAlign: 'center'}}>正在加载更多</Text>
                </View>
            );
        } else if (this.state.footer === 2) {
            return (
                <View
                    style={{
                        height: 30,
                        alignItems: 'center',
                        justifyContent: 'flex-start',
                    }}>
                    <Text
                        style={{
                            color: '#999999',
                            fontSize: 14,
                            marginTop: 5,
                            marginBottom: 5,
                        }}>
                        没有更多数据了
                    </Text>
                </View>
            );
        } else if (this.state.footer === 0) {
            return (
                <View
                    style={{
                        height: 30,
                        alignItems: 'center',
                        justifyContent: 'flex-start',
                    }}>
                    <Text
                        style={{
                            color: '#999999',
                            fontSize: 14,
                            marginTop: 5,
                            marginBottom: 5,
                        }}>
                        上拉加载更多
                    </Text>
                </View>
            );
        }
    }
    _renderItem(item) {
        return (
            <WingBlank
                style={{
                    backgroundColor: 'white',
                    marginTop: 10,
                    borderRadius: 10,
                }}>
                <Flex
                    onPress={() => {
                        this.props.navigation.push('ActivityInfo', {ParkingPlanID: item.ParkingPlanID});
                    }}
                    direction={'column'}
                    style={{
                        width: '100%',
                        paddingTop: '3%',
                        paddingBottom: 10,
                    }}>
                    <Flex
                        style={{
                            width: '100%',
                            paddingLeft: 15,
                            paddingRight: 15,
                            paddingTop: 9,
                        }}
                        justify={'between'}>
                        <Text
                            style={{
                                fontSize: 15,
                                color: '#333333',
                                fontWeight: 'bold',
                            }}>
                            活动名称：{}
                            <Text style={{color: '#333333'}}>{item.PlanName}</Text>
                        </Text>
                        <Text style={{color: 'red'}}>
                            {item.PlanState === 2 && '失效'}
                            {item.PlanState === 3 && '草稿'}
                            {item.PlanState === 4 && '取消'}
                            {item.PlanState === 5 && '作废'}
                            {item.PlanState === 6 && item.AgentState === 6 && '待审核'}
                            {item.PlanState === 6 && item.AgentState === 1 && '初审通过 平台待审'}
                            {item.PlanState === 6 && item.AgentState === 7 && '初审失败'}
                            {item.PlanState === 7 && '审核失败'}
                            {item.PlanState === 1 && item.ValidBegin < moment().format() && item.ValidEnd > moment().format() && (
                                <Text style={{color: 'red'}}>进行中</Text>
                            )}
                            {item.PlanState === 1 && item.ValidBegin > moment().format() && <Text style={{color: 'red'}}>待进行</Text>}
                            {item.PlanState === 1 && item.ValidEnd < moment().format() && <Text style={{color: 'red'}}>已完成</Text>}
                        </Text>
                    </Flex>
                    <View style={{width: '100%', height: 1, backgroundColor: '#EEEEEE', marginTop: 10}} />
                    <Flex
                        style={{
                            width: '100%',
                            paddingLeft: 15,
                            paddingRight: 15,
                            paddingTop: 9,
                        }}
                        justify={'between'}>
                        <Text
                            style={{
                                fontSize: 15,
                                color: '#666666',
                            }}>
                            楼盘名称
                        </Text>
                        <Text numberOfLines={1} style={{color: '#666666'}}>
                            {item.BuildingName}
                        </Text>
                    </Flex>
                    <Flex
                        style={{
                            width: '100%',
                            paddingLeft: 15,
                            paddingRight: 15,
                            paddingTop: 9,
                        }}
                        justify={'between'}>
                        <Text style={{fontSize: 15, color: '#666666'}}>联系人</Text>
                        <Text
                            style={{
                                color: '#666666',
                            }}>
                            {item.Contacter}
                        </Text>
                    </Flex>
                    <Flex
                        style={{
                            width: '100%',
                            paddingLeft: 15,
                            paddingRight: 15,
                            paddingTop: 9,
                        }}
                        justify={'between'}>
                        <Text
                            style={{
                                fontSize: 15,
                                color: '#666666',
                            }}>
                            联系电话
                        </Text>
                        <Text style={{color: '#666666'}}>{item.Telephone}</Text>
                    </Flex>
                    <View style={{width: '100%', height: 1, backgroundColor: '#EEEEEE', marginTop: 10}} />
                    <Flex
                        style={{
                            width: '100%',
                            height: 50,
                            paddingLeft: 15,
                            paddingRight: 15,
                            paddingTop: 9,
                        }}
                        justify={'between'}>
                        <Flex align={'start'}>
                            <Text style={{fontSize: 15, color: '#999999'}}>活动时间：</Text>
                            <View>
                                <Text
                                    style={{
                                        color: '#999999',
                                        fontSize: 14,
                                    }}>
                                    {moment(item.ValidBegin).format('YYYY.MM.DD HH:mm')}
                                </Text>
                                <Text
                                    style={{
                                        color: '#999999',
                                        fontSize: 14,
                                    }}>
                                    {moment(item.ValidEnd).format('YYYY.MM.DD HH:mm')}
                                </Text>
                            </View>
                        </Flex>
                        {item.PlanState === 6 && item.AgentState === 6 && this.props.userinfo.identity === 31 && (
                            <>
                                <Text
                                    style={styles.textbuttom5}
                                    onPress={() => {
                                        this.comfire(item.ParkingPlanID);
                                    }}>
                                    待审
                                </Text>
                                <Modal popup visible={this.state.visible} animationType="slide-up" onClose={this.onClose}>
                                    <Text style={{margin: 20}}>确认通过该活动的审核？</Text>
                                    <Text style={{padding: 20}}>审核意见</Text>
                                    <InputItem placeholder="请输入审核意见" placeholderTextColor="gray" type="text" onChange={this.setTextinfo} />
                                    <Flex justify={'around'}>
                                        <Button
                                            onPress={() => {
                                                this.comfireactivity(7);
                                            }}
                                            style={{
                                                width: '45%',
                                                marginTop: 30,
                                                backgroundColor: '#F2BC38',
                                                borderColor: '#F2BC38',
                                            }}
                                            type={'primary'}>
                                            审核不通过
                                        </Button>
                                        <Button
                                            style={{width: '45%', marginTop: 30}}
                                            type={'warning'}
                                            onPress={() => {
                                                this.comfireactivity(1);
                                            }}>
                                            审核通过
                                        </Button>
                                    </Flex>
                                </Modal>
                            </>
                        )}
                    </Flex>
                </Flex>
            </WingBlank>
        );
    }
    render() {
        return (
            <View style={{flex: 1}}>
                <Tabs tabs={this.state.tabs} prerenderingSiblingsNumber={0} initialPage={this.state.index} onChange={this.TabClick}>
                    <FlatList
                        data={this.state.list}
                        style={{width: '100%'}}
                        showsVerticalScrollIndicator={false}
                        renderItem={(data) => this._renderItem(data.item)}
                        keyExtractor={(item, index) => index}
                        // ListHeaderComponent={this._HeaderView}
                        ListFooterComponent={() => this.genIndicator()} //上拉加载更多视图
                        onEndReached={() => {
                            this.loadData();
                        }}
                        onEndReachedThreshold={0.1} // 这个属性的意思是 当滑动到距离列表内容底部不足 0.1*列表内容高度时触发onEndReached函数 为啥要加这个属性 因为不加的话滑动一点点就会立即触发onReached函数，看不到菊花加载中
                        refreshing={true}
                        refreshControl={
                            <RefreshControl
                                refreshing={this.state.refreshing}
                                onRefresh={() => {
                                    this._onRefresh();
                                }}
                            />
                        }
                    />
                </Tabs>
            </View>
        );
    }
}
const styles = StyleSheet.create({
    wingblank: {
        paddingTop: '2%',
        paddingBottom: '2%',
        borderWidth: 0.3,
        paddingLeft: '1%',
        paddingRight: '1%',
        marginBottom: '3%',
    },
    flextext: {
        paddingBottom: '1%',
    },
    textbuttom5: {
        fontSize: 14,
        width: 80,
        height: 30,
        borderWidth: 1,
        borderColor: '#Ec4f46',
        borderRadius: 15,
        color: '#Ec4f46',
        textAlign: 'center',
        paddingTop: 5,
        marginLeft: 5,
    },
});
export default connect(mapGetter)(Activity);
